package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Override
    @Transactional
    public void save(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //插入菜品数据
        dishMapper.insert(dish);
        //获取菜品id
        Long dishId = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            //插入口味数据
            dishFlavorsMapper.insertBatch(flavors);
        }
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult selectByPage(DishPageQueryDTO dishPageQueryDTO) {

        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.selectByPage();

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //判断所删菜品是否是启用状态
        Long count = dishMapper.countByStatus(ids);
        if(count > 0)
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);

        //判断所删菜品是否关联套餐
        count = setmealDishMapper.countByDishId(ids);
        if(count > 0)
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);

        //删除菜品
        dishMapper.deleteBatch(ids);

        //同时删除菜品关联口味
        dishFlavorsMapper.deleteBatch(ids);
    }

    /**
     * 根据id查询菜品及其关联口味
     * @param id
     * @return
     */
    @Override
    public DishVO selectByIdWithFlavor(Long id) {

        //根据id查询菜品
        Dish dish = dishMapper.selectById(id);

        //根据id查询菜品关联口味
        List<DishFlavor> dishFlavors = dishFlavorsMapper.selectById(id);

        //结果赋值给回显数据
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavors);

        return dishVO;
    }

    /**
     * 更新菜品及与之关联口味信息
     * @param dishDTO
     */
    @Transactional
    @Override
    public void updateWithFlavor(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        //更新菜品信息
        dishMapper.update(dish);

        //删除原有口味
        List<Long> id = new ArrayList<>();
        id.add(dishDTO.getId());
        dishFlavorsMapper.deleteBatch(id);

        //更新口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            //插入口味数据
            dishFlavorsMapper.insertBatch(flavors);
        }
    }

    /**
     * 启用或禁用菜品
     * @param status
     * @param id
     */
    @Override
    public void disableOrEnable(Integer status, Long id) {

        Dish dish = Dish.builder()
                .status(status)
                .id(id)
                .build();
        dishMapper.update(dish);
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> selectByCategoryId(Long categoryId) {

        List<Dish> list = dishMapper.selectByCategoryId(categoryId);

        return list;
    }

    /**
     * 根据种类id查询菜品及其相关口味
     * @param categoryId
     * @return
     */
    @Override
    public List<DishVO> selectWithFlavorByCategoryId(Long categoryId) {

        List<Dish> dishes = dishMapper.selectOnSaleByCategoryId(categoryId);
        List<DishVO> list = new ArrayList<>();
        for(Dish dish : dishes) {
            List<DishFlavor> flavors = dishFlavorsMapper.selectById(dish.getId());
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish, dishVO);
            dishVO.setFlavors(flavors);
            list.add(dishVO);
        }

        return list;
    }

}
